#include <ESP8266WiFi.h>
#include "Common.h"
#include "MyWiFi.h"
#include "MyWebServer.h"
#include "BlockFileHandler.h"

unsigned long _curTimer = 0;
unsigned long _wifiTimer = 0;
unsigned long _webTimer = 0;

MyWiFi      _myWiFi;
MyWebServer _myWeb;
String      _wifiConfig[2];


void setup()
{
  //初始化
  LOG("[Main] sys init...");
  Serial.begin(9600);
  SPIFFS.begin();

  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
  digitalWrite(0, LOW);
  digitalWrite(2, LOW);

  //读取配置
  BlockFileHandler bfh;
  bfh.read("/conf.txt", _wifiConfig, 2);

  //配置WiFi
  _myWiFi.configArg(_wifiConfig[0], _wifiConfig[1]);
}


void loop()
{
  _curTimer = millis();

  //WiFi TASK:
  if ((_curTimer - _wifiTimer) > WIFI_CYCLE) {
    _wifiTimer = _curTimer;
    _myWiFi.stateMachine();
  }

  //WebServer TASK:
  if ((_curTimer - _webTimer) > WEBSERVER_CYCLE) {
    _webTimer = _curTimer;
    _myWeb.stateMachine();
  }

  delay(2);
}
